package com.example.administrator.viewpager;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.listener.OnLoadMoreListener;
import com.example.administrator.myfragment.R;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

public class MyFragment2 extends Fragment {

    private RecyclerView recyclerView;
    private String fName;
    String id;
    int page = 1;

    ArticleListAdapter adapter;
    public MyFragment2() {

    }

    public MyFragment2(String fName, String id) {
        this.fName = fName;
        this.id = id;
    }
    private View parentView;

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (null == parentView) {
            parentView = inflater.inflate(R.layout.list, container, false);
            recyclerView = (RecyclerView) parentView.findViewById(R.id.recyclerview);
            initRecyclerView();
            loadData("aa");
        }
        Log.d("======" + fName, "onCreateView=="+(null == parentView?"true":"false"));
        return parentView;
    }

    private void initRecyclerView() {
        adapter = new ArticleListAdapter(getContext());
        recyclerView.setAdapter(adapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        adapter.getLoadMoreModule().setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                page++;
                loadData("initRecyclerView");
            }
        });
    }


    private void loadData(String type) {
        //这里进行网络请求和数据装载
        Log.d("=====" + fName + "=", "loadData===" + type);
        //60  169  269  529
        String url = "https://www.wanandroid.com/wxarticle/list/" + id + "/" + page + "/json";
        OkGo.<String>get(url)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {

                        BaseModel<ArticleList> model = new Gson().fromJson(response.body().toString(), BaseModel.class);
                        String json = new Gson().toJson(model.getData());
                        ArticleList list = new Gson().fromJson(json, ArticleList.class);
                        if (page == 1) {
                            adapter.setList(list.getDatas());
                        } else {
                            adapter.addData(list.getDatas());
                        }
                        if (list.getCurPage() >= list.getPageCount()) {
                            adapter.getLoadMoreModule().loadMoreEnd();
                        } else {
                            adapter.getLoadMoreModule().loadMoreComplete();
                        }
                    }

                    //这个方法默认是空实现，需要自己重写出来，不然你访问出了错你只能打印库中给你提供的打印日志
                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                    }
                });

    }


    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        Log.d("======" + fName, "onAttach");
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d("======" + fName, "onCreate");

    }

    @Override
    public void onStart() {
        super.onStart();
        Log.d("======" + fName, "onStart");
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.d("======" + fName, "onResume");
    }

    @Override
    public void onPause() {
        super.onPause();
        Log.d("======" + fName, "onPause");
    }

    @Override
    public void onStop() {
        super.onStop();
        Log.d("======" + fName, "onStop");
    }
    @Override
    public void onDestroyView() {
        Log.d("======" + fName, "onDestroyView");
        super .onDestroyView();
        if (null != parentView) {
            ((ViewGroup) parentView.getParent()).removeView(parentView);
        }
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d("======onDestroy" + fName, "onDestroy");
    }
}
